package cn.wxl.mymeituan.activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import cn.wxl.mymeituan.R;
import cn.wxl.mymeituan.adapter.ShopAdapter;
import cn.wxl.mymeituan.bean.ShopBean;
import cn.wxl.mymeituan.utils.Constants;
import cn.wxl.mymeituan.utils.JsonParse;
import cn.wxl.mymeituan.views.ShopListView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import cn.wxl.mymeituan.utils.GlideImageLoader;

import static cn.wxl.mymeituan.adapter.ShopAdapter.*;

public class ShopActivity extends AppCompatActivity {
    private TextView back, app_title;         //返回键与标题控件
    private ShopListView lv_shop;              //列表控件
    private ShopAdapter adapter;                //列表的适配器
    public static final int MSG_SHOP_OK = 1; //获取数据
    private MHandler mHandler;
    private RelativeLayout title_bar;
    private Context mContext;
    private List<ShopBean> sbl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        mHandler=new MHandler();
        initData();
        initBanner();
        init();
    }
    private void initBanner() {
        List<String> images = new ArrayList<>();
        //添加图片
        images.add("http://192.168.1.103:3000/order/img/banner/kuai_ke_ning_meng.jpg");
        images.add("http://192.168.1.103:3000/order/img/banner/wei_er_na_si.jpg");
        images.add("http://192.168.1.103:3000/order/img/banner/ben_gong_de_cha.jpg");
        Banner banner = (Banner) findViewById(R.id.banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        //增加点击事件
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                switch (position) {
                    case 0:
                        if (specialBean1 == null) return;
                        Intent intent1 = new Intent(ShopActivity.this, ShopDetailActivity.class);
                        intent1.putExtra("shop", specialBean1);
                        startActivity(intent1);
                        break;
                    case 1:
                        if (specialBean2 == null) return;
                        Intent intent2 = new Intent(ShopActivity.this, ShopDetailActivity.class);
                        intent2.putExtra("shop", specialBean2);
                        startActivity(intent2);
                        break;
                    case 2:
                        if (specialBean3 == null) return;
                        Intent intent3 = new Intent(ShopActivity.this, ShopDetailActivity.class);
                        intent3.putExtra("shop", specialBean3);
                        startActivity(intent3);
                        break;
                    default:
                        break;
                }
            }
        });
    }
    /**
     * 初始化界面控件
     */
    private void init(){
        back = (TextView) findViewById(R.id.back);
        app_title = (TextView) findViewById(R.id.app_title);
        app_title.setText("店铺");
        title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        title_bar.setBackgroundColor(getResources().getColor(R.color.blue_color));
        back.setVisibility(View.GONE);
        lv_shop = (ShopListView) findViewById(R.id.lv_shop);
        adapter=new ShopAdapter(this);
        lv_shop.setAdapter(adapter);
    }
    private void initData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Constants.WEB_SITE +
                Constants.REQUEST_SHOP_URL).build();
        Call call = okHttpClient.newCall(request);
        // 开启异步线程访问网络
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string(); //获取店铺数据
                Message msg = new Message();
                msg.what = MSG_SHOP_OK;
                msg.obj = res;
                mHandler.sendMessage(msg);
            }
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
        });

    }
    /**
     * 事件捕获
     */
    class MHandler extends Handler {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if (msg.what == MSG_SHOP_OK) {
                if (msg.obj != null) {
                    String vlResult = (String) msg.obj;
                    //解析获取的JSON数据
                    List<ShopBean> shopBeanList = JsonParse.getInstance().
                            getShopList(vlResult);
                    adapter.setData(shopBeanList);
                }
            }
        }
    }
    protected long exitTime;//记录第一次点击时的时间
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(ShopActivity.this, "再按一次退出订餐应用",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                ShopActivity.this.finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
